
package com.shop.cloud.mall.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.cloud.common.core.util.R;
import com.shop.cloud.common.log.annotation.SysLog;
import com.shop.cloud.mall.admin.service.VipCostService;
import com.shop.cloud.mall.admin.service.VipTypeService;
import com.shop.cloud.mall.common.entity.VipCost;
import com.shop.cloud.mall.common.entity.VipType;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 会员类型
 *
 * @author
 * @date 2024-03-19 15:17:11
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/viptype")
@Tag(description = "viptype", name = "会员类型管理")
public class VipTypeController {

    private final VipTypeService vipTypeService;
    private final VipCostService vipCostService;

    /**
     * 用户会员卡配置list
     * @param vipType
     * @return
     */
    @Operation(summary = "会员类型list")
    @GetMapping("/list")
    public R list(VipType vipType) {
        return R.ok(vipTypeService.list(Wrappers.query(vipType)));
    }


    /**
     * 会员类型分页列表
     * @param page 分页对象
     * @param vipType 会员类型
     * @return
     */
    @Operation(summary = "会员类型分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ato.hasAuthority('mall:viptype:index')")
    public R getPage(Page page, VipType vipType) {
        return R.ok(vipTypeService.page(page, Wrappers.query(vipType)));
    }

    /**
     * 会员类型查询
     * @param id
     * @return R
     */
    @Operation(summary = "会员类型查询")
    @GetMapping("/{id}")
    @PreAuthorize("@ato.hasAuthority('mall:viptype:get')")
    public R getById(@PathVariable("id") String id) {
        return R.ok(vipTypeService.getById(id));
    }

    /**
     * 会员类型新增
     * @param vipType 会员类型
     * @return R
     */
    @Operation(summary = "会员类型新增")
    @SysLog("新增会员类型")
    @PostMapping
    @PreAuthorize("@ato.hasAuthority('mall:viptype:add')")
    public R save(@RequestBody VipType vipType) {
        return R.ok(vipTypeService.save(vipType));
    }

    /**
     * 会员类型修改
     * @param vipType 会员类型
     * @return R
     */
    @Operation(summary = "会员类型修改")
    @SysLog("修改会员类型")
    @PutMapping
    @PreAuthorize("@ato.hasAuthority('mall:viptype:edit')")
    public R updateById(@RequestBody VipType vipType) {
        return R.ok(vipTypeService.updateById(vipType));
    }

    /**
     * 会员类型删除
     * @param id
     * @return R
     */
    @Operation(summary = "会员类型删除")
    @SysLog("删除会员类型")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ato.hasAuthority('mall:viptype:del')")
    public R removeById(@PathVariable String id) {
        long conut = vipCostService.count(Wrappers.<VipCost>lambdaQuery()
                .eq(VipCost::getVipTypeId,id));
        if(conut > 0){
            return R.failed("请先删除该会员类型的费用配置");
        }
        return R.ok(vipTypeService.removeById(id));
    }

}
